// pages/work/repCard/index.js
const util = require('../../../utils/util.js')
const datas = require("../../../data/data.js")
const my = require("../../../utils/MyUtils")
const log=require("../../../utils/log")
const app = getApp()
Page({
  /***
   * 定义数据类型
   * name  yctype
   * value  1--补卡,2--请假，3-外出 4-加班
   */
  /**
   * 页面的初始数据
   */
  data: {
    status1: true,
    status2: true,
    buttonStuts: false,
    tabScrollTop: 0,
    qkType: datas.qkType,
    length: 200, //固定长度
    nowlen: 0, //目前长度
    scrollHeight: null,
    biaodan: {
      lx: 0, //类型
      start: null, //开始时间
      zmr: null, //证明人
      num: 0, //天数
      info: null, //事由
      spr: '', //审批人
      csr: '' //抄送人
    },
    bgColor: '',
    list: [],
    list2: {}, //单个申请详情
  },
  /* JS */
  // 封装函数获取ID为box的元素实际高度 
  getScrollHeight: function () {
    wx.createSelectorQuery().select('.s').boundingClientRect((rect) => {
      this.setData({
        scrollHeight: rect.height
      })
    }).exec()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    var _TOKEN = wx.getStorageSync('_TOKEN')
    my.request("/oa-applet/initiate-application", "get", {}, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      t.setData({
        dept: res.data.data.department_name
      })
    })

  },

  getTabScrollTop: function (e) {
    this.setData({
      tabScrollTop: e.detail.y,
    })
  },
  //监听用户滑动页面事件
  onPageScroll: function (e) {
    var buttonStutes = false
    if (e.scrollTop > this.data.tabScrollTop) {
      // 滚动到最底部
      e.scrollTop = 0;
      buttonStutes = true;
    } else {
      e.scrollTop = 0;
      buttonStutes = false;
    }
    //给scrollTop重新赋值 
    this.setData({
      scrollTop: e.scrollTop,
      buttonStuts: buttonStutes
    })
  },
  /**
   * 查看补卡登记页面
   */
  bk: function () {
    var that = this
    if (!that.data.status1) {
      that.setData({
        status1: !this.data.status1,
      })
    }
  },
  /**
   * 查看补卡申请
   */
  ck: function () {
    var that = this
    var list = this.data.list
    var _TOKEN = wx.getStorageSync('_TOKEN')
    my.request("/oa-applet/application-record", "get", {
      apply_type: '5'
    }, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 0) {
        list = res.data.data
        for (var i = 0; i < list.length; i++) {
          list[i].ycType = 5
          list[i].shrq = list[i].date
        }
        that.setData({
          list
        })
      } else {
        my.alert(res.data.message)
      }
    })
    var stat = true
    var stat1 = false
    if (this.data.status1 == false) {
      stat1 = false
    }
    if (this.data.status2 == false) {
      stat = true
    }
    this.setData({
      status1: stat1,
      status2: stat
    })
  },

  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    var biaodan = this.data.biaodan
    biaodan.start = e.detail.value
    this.setData({
      biaodan
    })
  },
  /**
   * 获取缺卡类型
   * @param {*} e 
   */
  listenerPickerSelected: function (e) {
    //改变index值，通过setData()方法重绘界面
    var biaodan = this.data.biaodan
    biaodan.lx = e.detail.value
    this.setData({
      biaodan
    });
  },
  /**
   * 获取事由字数
   * @param {*} res 
   */
  getLength(res) {
    var n = res.detail.value;
    var len = n.length;
    var biaodan = this.data.biaodan
    biaodan.info = n
    this.setData({
      nowlen: len,
      biaodan
    })
  },
  inputChange(e) {
    var biaodan = this.data.biaodan
    e.currentTarget.dataset.id == "zmr" ? biaodan.zmr = e.detail.value : ''
    this.setData({
      biaodan
    })
  },
  onShow: function () {
    var biaodan = this.data.biaodan
    if (Object.keys(app.globalData.csr).length != 0) {
      biaodan.csr = app.globalData.csr.uid
      this.setData({
        biaodan,
        csrName: app.globalData.csr.use_name
      })
    }
    if (Object.keys(app.globalData.spr).length != 0) {
      biaodan.spr = app.globalData.spr.uid
      this.setData({
        biaodan,
        sprName: app.globalData.spr.use_name
      })
    }
  },
  spr(e) {
    wx.navigateTo({
      url: '../list/index?flag=sp',
    })
  },
  cs(e) {
    wx.navigateTo({
      url: '../list/index?flag=cs',
    })
  },
  submit() {
    var t = this
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var biaodan = this.data.biaodan
    biaodan.lx == 0 ? my.alert("缺卡类型不能为空") : (
      biaodan.start == null && biaodan.start == '' ? my.alert("时间为空") : (
        biaodan.zmr == null && biaodan.zmr == '' ? my.alert("证明人为空") : (
          biaodan.info == '' || biaodan.info == null ? my.alert("出差事由为空") : (
            biaodan.spr == '' ? my.alert("审批人为空") : my.request("/oa-applet/initiate-application-func?apply_type=5", "post",
              my.json2Form({
                patch_card_type: biaodan.lx,
                patch_card_time: biaodan.start,
                patch_card_witness: biaodan.zmr,
                patch_card_reason: biaodan.info,
                patch_card_approver_uid: biaodan.spr,
                patch_card_copyer_uid: biaodan.csr
              }), {
                token: _TOKEN,
                'content-type': 'application/x-www-form-urlencoded',
                'accept': 'application/json'
              },
              function (res) {
                wx.hideLoading()
                log.info(my.info(new Date(),wx.getStorageSync('userInfo').user_name,"提交了补卡申请"))
                biaodan.start = ""
                biaodan.end = ""
                biaodan.num = ""
                biaodan.lx = 0
                biaodan.info = ""
                biaodan.spr = ""
                biaodan.zmr = ""
                biaodan.csr = ""
                app.globalData.spr = {}
                app.globalData.csr = {}
                t.setData({
                  biaodan,
                  sprName:"",
                  csrName:""
                })
                my.alert(res.data.message)
              })
          )
        )
      )
    )
  },
  num(e) {
    var biaodan = this.data.biaodan
    // console
    if (e.detail.value) {
      biaodan.num = e.detail.value
    }
    this.setData({
      biaodan
    })
  },
  selectOne(e) {
    wx.navigateTo({
      url: '../xaingqing/index?apply_type=5&apply_id='+e.currentTarget.dataset.id,
    })
  },
  onUnload: function () {
    app.globalData.csr = {}
    app.globalData.spr = {}
  }
})